/**
 * 
 */
package siena.dbslayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * This is a simple serializer for plain text.
 * @author jsanca
 * 
 */
public class PlainTextSerializer implements Serializer {

	/**
	 * Constructor.
	 */
	public PlainTextSerializer() {

		super ();
	} // PlainTextSerializer.

	/*
	 * (non-Javadoc)
	 * 
	 * @see siena.dbslayer.Serializer#deserialize(java.io.InputStream)
	 */
	public Object deserialize(InputStream in) throws IOException {

		StringBuilder output = new StringBuilder();
		String line = null;
		BufferedReader inReader = null;

		try {
			inReader = new BufferedReader(new InputStreamReader(in));
			while (null != (line = inReader.readLine())) {

				output.append(line);
			}
		} finally {
			if (null != inReader) {
				inReader.close();
			}
		}

		return output.toString();
	} // deserialize.

	/*
	 * (non-Javadoc)
	 * 
	 * @see siena.dbslayer.Serializer#serialize(java.lang.Object,
	 *      java.io.OutputStream)
	 */
	public void serialize(Object document, OutputStream out) throws IOException {

		PrintStream outStream = new PrintStream(out);
		outStream.println(document);
	} // serialize.

} // E:O:F:PlainTextSerializer.
